﻿//动作控制
//对一Dom进行事件控制
Function.registerNameSpace("Page");

Page.Action=function(obj){
    Function.registerClass(this);
    if(typeof(obj)=="string") obj=$$(obj);
    this.object=obj;
    this._fixedPosition=null;    
    
   	//动画状态
	this._STOPED_="stoped";
	this._PLAYING_="playing";
	this._PAUSING_="pausing";

	//动画参数存储
	this._numericStyles={};
	this._colorStyles={};
	this._fps=25;
	this._duration=1;
	this._percentDelta=null;
	this._percentComplete=0;
	this._styleActionMode=this._STOPED_;
}

//让dom显示在页面上的一个固定位置
Page.Action.prototype.ToFixedPosition = function(position) {
    this._fixedPosition = position;

    if (this.__setCenterPosition) {//如果居中过，则删除
        EventOperate.delEvent(window, "resize", this.__setCenterPosition);
        EventOperate.delEvent(window, "scroll", this.__setCenterPosition);
        this.__setCenterPosition = null;
        delete this.__setCenterPosition;
    }
    if (!this._testPosition()) {//不支持fixed
        if (!this.__setPosition) {
            this.__setPosition = EventOperate.createDelegate(this, this.___setPosition);
            EventOperate.addEvent(window, "scroll", this.__setPosition);
            EventOperate.addEvent(window, "resize", this.__setPosition);
        }
        this.___setPosition();
    } else {//支持fixed
        this.___setPosition2();
    }
}

//测试支不支持fixed
Page.Action.prototype._testPosition=function(){
    if(document.IsIE){ 
        if(document.IsIE6 || document.compatMode=="BackCompat") return false;
        else return true;
    }
    return true;
}

//让dom显示在页面上固定居中
Page.Action.prototype.ToFixedCenter=function(){
    if(this.__setPosition){//如果设过固定位置，则删除
        EventOperate.delEvent(window,"resize",this.__setPosition);
        EventOperate.delEvent(window,"scroll",this.__setPosition);
        this.__setPosition=null;
        delete this.__setPosition;
    }
    if(!this.__setCenterPosition){
        this.__setCenterPosition=EventOperate.createDelegate(this,this.___setCenterPosition);
        EventOperate.addEvent(window,"resize",this.__setCenterPosition);
        EventOperate.addEvent(window,"scroll",this.__setCenterPosition);
    }
    this.___setCenterPosition();
}

//ie6兼容的
Page.Action.prototype.___setPosition=function(sender,e){
    if(this._fixedPosition==null) return;
    this.object.style.position="absolute";
    this.object.style.right="";
    this.object.style.bottom="";
    if(this._fixedPosition.left!=null) this.object.style.left=(document.getScrollLeft()+this._fixedPosition.left)+"px";
    if(this._fixedPosition.right!=null) this.object.style.left=(document.getScrollLeft()+document.getClientWidth()-this._fixedPosition.right-this.object.offsetWidth)+"px";
    if(this._fixedPosition.top!=null) this.object.style.top=(document.getScrollTop()+this._fixedPosition.top)+"px";
    if(this._fixedPosition.bottom!=null) this.object.style.top=(document.getScrollTop()+document.getClientHeight()-this._fixedPosition.bottom-this.object.offsetHeight)+"px";
}

//其他浏览器
Page.Action.prototype.___setPosition2 = function() {
    if (this._fixedPosition == null) return;
    this.object.style.position = "fixed";
    if (this._fixedPosition.left != null) this.object.style.left = this._fixedPosition.left + "px";
    else this.object.style.left = "auto";
    if (this._fixedPosition.right != null) this.object.style.right = this._fixedPosition.right + "px";
    else this.object.style.right = "auto";
    if (this._fixedPosition.top != null) this.object.style.top = this._fixedPosition.top + "px";
    else this.object.style.top = "auto";
    if (this._fixedPosition.bottom != null) this.object.style.bottom = this._fixedPosition.bottom + "px";
    else this.object.style.bottom = "auto";
}

Page.Action.prototype.___setCenterPosition=function(sender,e){
    var left=Math.floor((document.getClientWidth()-this.object.offsetWidth)/2);
    var top=Math.floor((document.getClientHeight()-this.object.offsetHeight)/2);
    this._fixedPosition={left:left,top:top};
    if(this._testPosition()) this.___setPosition2();
    else  this.___setPosition();
}

//按优先度下，上，左，右的须序安排位置
//必须让本对象处于可度量状态
Page.Action.prototype.AutoSide=function(target){
    if(typeof(target)=="string") target=$$(target);
    var left=document.getObjLeft(target);
    var top=document.getObjTop(target);
    var objWidth=this.object.offsetWidth;
    var objHeight=this.object.offsetHeight;
    var targetWidth=target.offsetWidth;
    var targetHeight=target.offsetHeight;
    var clientWidth=document.getClientWidth();
    var clientHeight=document.getClientHeight();
    
    var scrollTop=document.getScrollTop();
    var scrollLeft=document.getScrollLeft();
    var maxRight=scrollLeft+clientWidth;
    var maxBottom=scrollTop+clientHeight;
    
    //测试
    var l=left;
    var t=top+targetHeight;
    var b=t+objHeight;
    var r=l+objWidth;
    
    var isBottom1=top+targetHeight+objHeight-3>maxBottom;
    var isBottom2=top+objHeight-3>maxBottom;
    var isTop1=top-objHeight+3<scrollTop;
    var isTop2=top-objHeight+targetHeight+3<scrollTop;
    var isRight1=left+targetWidth+objWidth-3>maxRight;
    var isRight2=left+objWidth-3>maxRight;
    var isLeft1=left-objWidth+3<scrollLeft;
    var isLeft2=left-objWidth+targetWidth+3<scrollLeft;
    
    if(isBottom1){
        t=top-objHeight;
        if(isTop1){;
            if(!isLeft1||!isRight1){
                if(!isBottom2) t=top;
                else if(!isTop2) t=top-objHeight;
                else t=top+targetHeight;
            }else t=top+targetHeight;
        }    
    }
    if(left+3<scrollLeft){
        if(!isRight1) l=left+targetWidth;
        else if(objWidth<targetWidth) l=left+(targetWidth-objWidth);
    }
    if(left+objWidth-3>maxRight){
        if(!isLeft1) l=left-objWidth;
    }
    document.setStyle(this.object,{left:l+"px",top:t+"px"});
}


//添加一个动画要转换的参数(可数字化的)
//样式名,开始值，结束值，单位
Page.Action.prototype.AddNumericStyleAction=function(styleName,startValue,endValue,unit){
	this._numericStyles[styleName]={startValue:startValue,endValue:endValue,unit:unit};
}

//添加一个动画要转换的参数(与颜色有关的)
//样式名,开始值，结束值
//颜色值的要示{red:value,blue:value,green:value}
Page.Action.prototype.AddColorStyleAction=function(styleName,startValue,endValue){
	this._colorStyles[styleName]={startValue:startValue,endValue:endValue};
}

//动画开始
//duration完成动作的时间，以秒记
//fps 执行动作的频率，数字越大动作越平滑，但对性能要求越高
Page.Action.prototype.StyleActionPlay=function(duration,fps){
	if(this._styleActionMode==this._PLAYING_) return;//播放当中
	if(this._styleActionMode==this._STOPED_){//已停止初始化
		if(fps) this._fps=fps;
		if(duration) this._duration=duration;
		this._percentComplete=0;
		this._percentDelta=100/(this._duration*this._fps);
	}
	//开始动画
	if(!this._actionHandler) this._actionHandler=Function.createDelegate(this,this._styleActionStep);
	if(!this._sto) this._sto=setInterval(this._actionHandler,1000/this._fps);
	this._styleActionUpdatePercent(this._percentComplete);
	this._styleActionMode=this._PLAYING_;
}

//是否动画进行中
Page.Action.prototype.IsPlaying=function(){
    return (this._styleActionMode==this._PLAYING_);
}

//暂停
Page.Action.prototype.StyleActionPause=function(){
	if(this._styleActionMode==this._PAUSING_) return;//播放当中
	clearInterval(this._sto);
	delete this._sto;
	this._sto=null;
	this._styleActionMode=this._PAUSING_;
}

//结束动画
Page.Action.prototype.StyleActionStop=function(){
	if(this._styleActionMode==this._STOPED_) return;
	clearInterval(this._sto);
	delete this._sto;
	this._sto=null;
	this._percentComplete=100;
	this._styleActionRunning(this._percentComplete);
	this._numericStyles={};
	this._colorStyles={};
	this._styleActionMode=this._STOPED_;
	var func=EventOperate.getHandler(this,"Stoped");
	if(func) func(this,null);
	delete func;
	func=null;
}

Page.Action.prototype._styleActionStep=function(){
	this._percentComplete+=this._percentDelta
	this._styleActionUpdatePercent(this._percentComplete);
}

//更新数值
Page.Action.prototype._styleActionUpdatePercent=function(percent){
	if(percent>100) percent=100;
	if(percent==100) this.StyleActionStop();//动画结束
	this._styleActionRunning(percent);
}

//更新style的属性
Page.Action.prototype._styleActionRunning=function(percent){
	var styles={};
	for(var item in this._numericStyles){
		var o=this._numericStyles[item];
		if(o.unit===null||o.unit==="") styles[item]=o.startValue+(o.endValue-o.startValue)*percent/100;
		else styles[item]=(o.startValue+(o.endValue-o.startValue)*percent/100)+o.unit;
	}
	for(var item in this._colorStyles){
		var o=this._colorStyles[item];
		var red=o.startValue.red+Math.floor((o.endValue.red-o.startValue.red)*percent/100);
		var green=o.startValue.green+Math.floor((o.endValue.green-o.startValue.green)*percent/100);
		var blue=o.startValue.blue+Math.floor((o.endValue.blue-o.startValue.blue)*percent/100);
		styles[item]="rgb("+red+","+green+","+blue+")";
	}
	document.setStyle(this.object,styles);
}

//动画完成产生的事件
Page.Action.prototype.Add_StopedHandler=function(func){
	EventOperate.addHandler(this,"Stoped",func);
}
Page.Action.prototype.Remove_StopedHandler=function(func){
	EventOperate.removeHandler(this,"Stoped",func);
}

Page.Action.prototype.dispose=function(){
    if(this.__setCenterPosition){
        EventOperate.delEvent(window,"resize",this.__setCenterPosition);
        EventOperate.delEvent(window,"scroll",this.__setCenterPosition);
        delete this.__setCenterPosition;
        this.__setCenterPosition=null;
    }
    if(this.__setPosition){
        EventOperate.delEvent(window,"resize",this.__setPosition);
        EventOperate.delEvent(window,"scroll",this.__setPosition);
        delete this.__setPosition;this.__setPosition=null;
    }
  	if(this._sto){
    	clearInterval(this._sto)
    	delete this._sto;this._sto=null;
	}
	delete this._numericStyles;this._numericStyles=null;
	delete this._colorStyles;this._colorStyles=null;
	if(this._actionHandler){delete this._actionHandler;this._actionHandler=null;}
    delete this.object;this.object=null;
    Function.recycleClass(this);
}